<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是FatFs文件系统移植和基本使用相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV16-22-FatFs-02-FatFs文件系统移植">
<meta property="og:url" content="https://sumumm.github.io/post/fc1f4d1a.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是FatFs文件系统移植和基本使用相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/image-20230602224918758.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/image-20230602230210319.png">
<meta property="article:published_time" content="2023-06-04T14:07:22.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.012Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV16-STM32开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/image-20230602224918758.png">


<link rel="canonical" href="https://sumumm.github.io/post/fc1f4d1a.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/fc1f4d1a.html","path":"post/fc1f4d1a.html","title":"LV16-22-FatFs-02-FatFs文件系统移植"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV16-22-FatFs-02-FatFs文件系统移植 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81API%E4%B8%8E%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="nav-text">一、API与返回值</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%A4%E4%B8%AA%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-text">1. 两个结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-FATFS"><span class="nav-text">1.1 FATFS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-FIL"><span class="nav-text">1.2 FIL</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="nav-text">2. 函数返回值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E8%BF%94%E5%9B%9E%E5%80%BC%E7%9A%84%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B"><span class="nav-text">2.1 返回值的枚举类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%BB%84%EF%BC%9F"><span class="nav-text">2.2 字符串数组？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0"><span class="nav-text">3. 常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-f-mount"><span class="nav-text">3.1 f_mount()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-f-mkfs"><span class="nav-text">3.2 f_mkfs()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%97%B6%E9%97%B4%E5%87%BD%E6%95%B0get-fattime"><span class="nav-text">4. 时间函数get_fattime()</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9F%BA%E4%BA%8ESPI-FLASH%E7%9A%84%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="nav-text">二、基于SPI FLASH的文件系统</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%BA%95%E5%B1%82%E8%AF%BB%E5%86%99"><span class="nav-text">1. 底层读写</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1.1 初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E8%8E%B7%E5%8F%96SPI-FLASH%E5%99%A8%E4%BB%B6ID"><span class="nav-text">1.2 获取SPI FLASH器件ID</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="nav-text">1.3 读取数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE"><span class="nav-text">1.4 写入数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-diskio-c%E6%96%87%E4%BB%B6%E7%9A%84%E7%A7%BB%E6%A4%8D"><span class="nav-text">2. diskio.c文件的移植</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-drive-number"><span class="nav-text">2.1 drive number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-disk-initialize"><span class="nav-text">2.2 disk_initialize()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-text">2.2.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-%E5%AF%B9SPI-FLASH%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.2.2 对SPI FLASH初始化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C"><span class="nav-text">2.2.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-disk-status"><span class="nav-text">2.3 disk_status()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-text">2.3.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2-%E5%AF%B9SPI-FLASH%E7%9A%84%E7%8A%B6%E6%80%81%E6%A3%80%E6%B5%8B"><span class="nav-text">2.3.2 对SPI FLASH的状态检测</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C"><span class="nav-text">2.3.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-disk-read"><span class="nav-text">2.4 disk_read()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-text">2.4.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-2-%E4%BB%8ESPI-FLASH%E8%AF%BB%E5%8F%96"><span class="nav-text">2.4.2 从SPI FLASH读取</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C"><span class="nav-text">2.4.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-disk-write"><span class="nav-text">2.5 disk_write()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-text">2.5.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-%E5%90%91SPI-FLASH%E5%86%99%E5%85%A5"><span class="nav-text">2.5.2 向SPI FLASH写入</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C"><span class="nav-text">2.5.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-disk-ioctl"><span class="nav-text">2.6 disk_ioctl()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-text">2.6.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-2-%E8%8E%B7%E5%8F%96SPI-FLASH%E4%BF%A1%E6%81%AF"><span class="nav-text">2.6.2 获取SPI FLASH信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C"><span class="nav-text">2.6.3 移植结果</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ffconf-h%E6%96%87%E4%BB%B6%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="nav-text">3. ffconf.h文件的配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-FF-VOLUMES"><span class="nav-text">3.1 FF_VOLUMES</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-FF-MAX-SS"><span class="nav-text">3.2 FF_MAX_SS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-FF-USE-MKFS"><span class="nav-text">3.3 FF_USE_MKFS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-SPI-FLASH%E7%9A%84%E4%B8%80%E4%B8%AABUG"><span class="nav-text">3.4 SPI FLASH的一个BUG</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%9F%BA%E4%BA%8ESD%E5%8D%A1%E7%9A%84%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="nav-text">三、基于SD卡的文件系统</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%BA%95%E5%B1%82%E8%AF%BB%E5%86%99-1"><span class="nav-text">1. 底层读写</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%88%9D%E5%A7%8B%E5%8C%96-1"><span class="nav-text">1.1 初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E8%8E%B7%E5%8F%96SD%E5%8D%A1%E4%BF%A1%E6%81%AF"><span class="nav-text">1.2 获取SD卡信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE-1"><span class="nav-text">1.3 读取数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE-1"><span class="nav-text">1.4 写入数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-diskio-c%E6%96%87%E4%BB%B6%E7%9A%84%E7%A7%BB%E6%A4%8D-1"><span class="nav-text">2. diskio.c文件的移植</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-drive-number-1"><span class="nav-text">2.1 drive number</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-disk-initialize-1"><span class="nav-text">2.2 disk_initialize()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E-1"><span class="nav-text">2.2.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-%E5%AF%B9SD%E5%8D%A1%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.2.2 对SD卡初始化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C-1"><span class="nav-text">2.2.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-disk-status-1"><span class="nav-text">2.3 disk_status()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E-1"><span class="nav-text">2.3.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2-%E5%AF%B9SD%E5%8D%A1%E7%9A%84%E7%8A%B6%E6%80%81%E6%A3%80%E6%B5%8B"><span class="nav-text">2.3.2 对SD卡的状态检测</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C-1"><span class="nav-text">2.3.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-disk-read-1"><span class="nav-text">2.4 disk_read()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E-1"><span class="nav-text">2.4.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-2-%E4%BB%8ESD%E5%8D%A1%E8%AF%BB%E5%8F%96"><span class="nav-text">2.4.2 从SD卡读取</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C-1"><span class="nav-text">2.4.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-disk-write-1"><span class="nav-text">2.5 disk_write()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E-1"><span class="nav-text">2.5.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-%E5%90%91SD%E5%8D%A1%E5%86%99%E5%85%A5"><span class="nav-text">2.5.2 向SD卡写入</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C-1"><span class="nav-text">2.5.3 移植结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-disk-ioctl-1"><span class="nav-text">2.6 disk_ioctl()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-1-%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E-1"><span class="nav-text">2.6.1 函数声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-2-%E8%8E%B7%E5%8F%96SD%E5%8D%A1%E4%BF%A1%E6%81%AF"><span class="nav-text">2.6.2 获取SD卡信息</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-3-%E7%A7%BB%E6%A4%8D%E7%BB%93%E6%9E%9C-1"><span class="nav-text">2.6.3 移植结果</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-ffconf-h%E6%96%87%E4%BB%B6%E7%9A%84%E9%85%8D%E7%BD%AE-1"><span class="nav-text">3. ffconf.h文件的配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-FF-VOLUMES-1"><span class="nav-text">3.1 FF_VOLUMES</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-FF-MAX-SS-1"><span class="nav-text">3.2 FF_MAX_SS</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-FF-USE-MKFS-1"><span class="nav-text">3.3 FF_USE_MKFS</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E4%B8%80%E4%BA%9B%E6%B5%8B%E8%AF%95%E5%87%BD%E6%95%B0"><span class="nav-text">四、一些测试函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9A%E4%B9%89"><span class="nav-text">1. 文件系统结构体定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%94%B3%E8%AF%B7%E5%86%85%E5%AD%98"><span class="nav-text">2. 申请内存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E8%8E%B7%E5%8F%96%E7%A3%81%E7%9B%98%E5%AE%B9%E9%87%8F"><span class="nav-text">3. 获取磁盘容量</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/fc1f4d1a.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV16-22-FatFs-02-FatFs文件系统移植 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV16-22-FatFs-02-FatFs文件系统移植
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-04 22:07:22" itemprop="dateCreated datePublished" datetime="2023-06-04T22:07:22+08:00">2023-06-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV16-STM32开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>11k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>40 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是FatFs文件系统移植和基本使用相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table><tr><td align="center">分类  </td><td align="center">网址</td><td align="center">说明</td></tr><tr><td align="center" rowspan="4">官方网站</td><td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td><td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td></tr><tr>                                            <td align="left"><a href="https://www.st.com/content/st_com/zh.html" target="_blank">https://www.st.com/content/st_com/zh.html</a></td><td align="left">ST官方网站，在这里我们可以找到STM32的相关文档</td></tr><tr>                                            <td align="left"><a href="https://www.stmcu.com.cn/" target="_blank">https://www.stmcu.com.cn/</a></td><td align="left">意法半导体ST中文官方网站，在这里我们可以找到STM32的相关中文参考文档</td></tr><tr>                                            <td align="left"><a href="http://elm-chan.org/fsw/ff/00index_e.html" target="_blank">http://elm-chan.org/fsw/ff/00index_e.html</a></td><td align="left">FatFs文件系统官网</td></tr><tr><td align="center" rowspan="3">教程书籍</td><td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M3权威指南(中文).pdf" target="_blank">《ARM Cortex-M3权威指南》</a></td><td alirn="left" rowspan="3">ARM公司专家Joseph Yiu（姚文祥）的力作，中文翻译是NXP的宋岩</td></tr><tr>                                            <td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M0权威指南(中文).pdf" target="_blank">《ARM Cortex-M0权威指南》</a></td></tr><tr>                                            <td align="left"><a href="" target="_blank">《ARM Cortex-M3与Cortex-M4权威指南》</a></td></tr><tr><td align="center" rowspan="4">开发论坛</td><td align="left"><a href="http://47.111.11.73/forum.php" target="_blank">http://47.111.11.73/forum.php</a></td><td align="left">开源电子网，正点原子的资料下载及问题讨论论坛</td></tr><tr>                                            <td align="left"><a href="https://www.firebbs.cn/forum.php" target="_blank">https://www.firebbs.cn/forum.php</a></td><td align="left">国内Kinetis开发板-野火/秉火（刘火良）主持的论坛，现也做STM32和i.MX RT</td></tr><tr>                                            <td align="left"><a href="https://www.amobbs.com/index.php" target="_blank">https://www.amobbs.com/index.php</a></td><td align="left">阿莫（莫进明）主持的论坛，号称国内最早最火的电子论坛，以交流Atmel AVR系列单片机起家，现已拓展到嵌入式全平台，其STM32系列帖子有70W+。</td></tr><tr>                                            <td align="left"><a href="http://download.100ask.net/index.html" target="_blank">http://download.100ask.net/index.html</a></td><td align="left">韦东山嵌入式资料中心，有些STM32和linux的相关资料也可以来这里找。</td></tr><tr><td align="center" rowspan="3">博客参考</td><td align="left"><a href="http://www.openedv.com/" target="_blank">http://www.openedv.com/</a></td><td align="left">开源网-原子哥个人博客</td></tr><tr>                                            <td align="left"><a href="http://blog.chinaaet.com/jihceng0622" target="_blank">http://blog.chinaaet.com/jihceng0622</a></td><td align="left">博主是原Freescale现NXP的现场应用工程师</td></tr><tr>                                            <td align="left"><a href="https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/cortex-m-resources" target="_blank">cortex-m-resources</a></td><td align="left">这其实并不算是一个博客，这是ARM公司专家Joseph Yiu收集整理的所有对开发者有用的官方Cortex-M资料链接（也包含极少数外部资源链接）</td></tr></table><ul><li>STM32</li></ul><table>    <tr><td align="center" rowspan="2">STM32</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/index.html" target="_blank">STM32 HAL库开发实战指南——基于F103系列开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr><tr>                                            <td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/index.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr></table><ul><li>SD卡</li></ul><table>    <tr><td align="left"><a href="https://www.sdcard.org/" target="_blank">SD Association</a></td><td align="left">提供了SD存储卡和SDIO卡系统规范</td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx英文数据手册</a></td><td align="left">STM32F103xC/D/E系列的英文数据手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xC/D/E系列的中文数据手册</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx英文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的英文参考手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx中文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的中文参考手册</td></tr>    <tr><td align="left"><a href="https://developer.arm.com/documentation/100165/0201/?lang=en" target="_blank">Arm Cortex-M3 处理器技术参考手册-英文版</a></td><td align="left">Cortex-M3技术参考手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf" target="_blank">STM32F10xxx Cortex-M3编程手册-英文版(PM0056)</a></td><td align="left">STM32F10xxx/20xxx/21xxx/L1xxxx系列Cortex-M3编程手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/" target="_blank">SD卡相关资料——最新版本</a></td><td align="left">有关SD卡的一些资料可以从这里下载</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/archives/" target="_blank">SD卡相关资料——历史版本</a></td><td align="left">有关SD卡的一些历史版本资料可以从这里下载，比如后边看的SD卡2.0协议</td></tr>    <tr><td align="left"><a href="./" target="_blank">SD 2.0 协议标准完整版</a></td><td align="left">这是一篇关于SD卡2.0协议的中文文档，还是比较有参考价值的，可以一看</td></tr></table>
              </div>
            </details>

<p>注意这里的代码可能会用到动态管理内存的东西，可以看后边的笔记。</p>
<h1 id="一、API与返回值"><a href="#一、API与返回值" class="headerlink" title="一、API与返回值"></a><font size=3>一、API与返回值</font></h1><p>我们来看几个函数，以及结构体，方便后边测试和配置的时候使用。</p>
<h2 id="1-两个结构体"><a href="#1-两个结构体" class="headerlink" title="1. 两个结构体"></a><font size=3>1. 两个结构体</font></h2><h3 id="1-1-FATFS"><a href="#1-1-FATFS" class="headerlink" title="1.1 FATFS"></a><font size=3>1.1 FATFS</font></h3><p>关于该结构体，它定义在ff.h文件中，我们可以参考官方文档：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/sfatfs.html">FatFs - FATFS (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Filesystem object structure (FATFS) */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">	BYTE	fs_type;		<span class="comment">/* Filesystem type (0:not mounted) */</span></span><br><span class="line">	BYTE	pdrv;			<span class="comment">/* Volume hosting physical drive */</span></span><br><span class="line">	BYTE	ldrv;			<span class="comment">/* Logical drive number (used only when FF_FS_REENTRANT) */</span></span><br><span class="line">	BYTE	n_fats;			<span class="comment">/* Number of FATs (1 or 2) */</span></span><br><span class="line">	<span class="comment">// 中间的部分省略</span></span><br><span class="line">	BYTE	win[FF_MAX_SS];	<span class="comment">/* Disk access window for Directory, FAT (and file data at tiny cfg) */</span></span><br><span class="line">&#125; FATFS;</span><br></pre></td></tr></table></figure>

<p>FATFS结构(文件系统对象)保存各个逻辑驱动器的动态工作区域。它由应用程序给出，并通过f_mount函数注册&#x2F;取消注册到FatFs模块。结构的初始化在必要时由卷挂载进程完成。应用程序不得修改此结构中的任何成员，否则将导致FAT卷崩溃。</p>
<p>我们要注意一下这个结构体是非常大的，我们可以看到最后一个成员win，这是一个数组，大小为FF_MAX_SS，这个最大值我们后边可能会配置为4096，这样下来这个结构体就非常的大，用它定义的变量最好不要定义为局部变量，否则可能会导致栈的溢出，当然，要是我们的栈非常大的话吗，就可以不用考虑这个问题了。</p>
<h3 id="1-2-FIL"><a href="#1-2-FIL" class="headerlink" title="1.2 FIL"></a><font size=3>1.2 FIL</font></h3><p>关于该结构体，它定义在ff.h中，我们可以参考官方文档：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/sfile.html">FatFs - FIL (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* File object structure (FIL) */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">	FFOBJID	obj;			<span class="comment">/* Object identifier (must be the 1st member to detect invalid object pointer) */</span></span><br><span class="line">	BYTE	flag;			<span class="comment">/* File status flags */</span></span><br><span class="line">	BYTE	err;			<span class="comment">/* Abort flag (error code) */</span></span><br><span class="line">	FSIZE_t	fptr;			<span class="comment">/* File read/write pointer (Zeroed on file open) */</span></span><br><span class="line">	DWORD	clust;			<span class="comment">/* Current cluster of fpter (invalid when fptr is 0) */</span></span><br><span class="line">	LBA_t	sect;			<span class="comment">/* Sector number appearing in buf[] (0:invalid) */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> !FF_FS_READONLY</span></span><br><span class="line">	LBA_t	dir_sect;		<span class="comment">/* Sector number containing the directory entry (not used at exFAT) */</span></span><br><span class="line">	BYTE*	dir_ptr;		<span class="comment">/* Pointer to the directory entry in the win[] (not used at exFAT) */</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> FF_USE_FASTSEEK</span></span><br><span class="line">	DWORD*	cltbl;			<span class="comment">/* Pointer to the cluster link map table (nulled on open, set by application) */</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> !FF_FS_TINY</span></span><br><span class="line">	BYTE	buf[FF_MAX_SS];	<span class="comment">/* File private data read/write window */</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">&#125; FIL;</span><br></pre></td></tr></table></figure>

<p>FIL结构(文件对象)保存打开文件的状态。它由f_open函数创建，并由f_close函数丢弃。应用程序不能修改该结构中除cltbl之外的任何成员，否则将导致FAT卷崩溃。请注意，扇区缓冲区是在非微小配置(FF_FS_TINY &#x3D;&#x3D; 0)的结构中定义的，因此该配置中的FIL结构不应该被定义为自动变量。</p>
<p>我们要注意一下这个结构体是非常大的，我们可以看到最后一个成员buf，这是一个数组，大小为FF_MAX_SS，这个最大值我们后边可能会配置为4096，这样下来这个结构体跟FATFS一样，就非常的大，所以一般我们也不会将其定义为局部变量。</p>
<h2 id="2-函数返回值"><a href="#2-函数返回值" class="headerlink" title="2. 函数返回值"></a><font size=3>2. 函数返回值</font></h2><h3 id="2-1-返回值的枚举类型"><a href="#2-1-返回值的枚举类型" class="headerlink" title="2.1 返回值的枚举类型"></a><font size=3>2.1 返回值的枚举类型</font></h3><p>我们首先来了解一下我们常用的一些函数的返回值的含义，我们可以通过这些返回值来判断是哪里出错了：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* File function return code (FRESULT) */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">enum</span> &#123;</span></span><br><span class="line">	FR_OK = <span class="number">0</span>,				<span class="comment">/* (0) Succeeded */</span></span><br><span class="line">	FR_DISK_ERR,			<span class="comment">/* (1) A hard error occurred in the low level disk I/O layer */</span></span><br><span class="line">	FR_INT_ERR,				<span class="comment">/* (2) Assertion failed */</span></span><br><span class="line">	FR_NOT_READY,			<span class="comment">/* (3) The physical drive cannot work */</span></span><br><span class="line">	FR_NO_FILE,				<span class="comment">/* (4) Could not find the file */</span></span><br><span class="line">	FR_NO_PATH,				<span class="comment">/* (5) Could not find the path */</span></span><br><span class="line">	FR_INVALID_NAME,		<span class="comment">/* (6) The path name format is invalid */</span></span><br><span class="line">	FR_DENIED,				<span class="comment">/* (7) Access denied due to prohibited access or directory full */</span></span><br><span class="line">	FR_EXIST,				<span class="comment">/* (8) Access denied due to prohibited access */</span></span><br><span class="line">	FR_INVALID_OBJECT,		<span class="comment">/* (9) The file/directory object is invalid */</span></span><br><span class="line">	FR_WRITE_PROTECTED,		<span class="comment">/* (10) The physical drive is write protected */</span></span><br><span class="line">	FR_INVALID_DRIVE,		<span class="comment">/* (11) The logical drive number is invalid */</span></span><br><span class="line">	FR_NOT_ENABLED,			<span class="comment">/* (12) The volume has no work area */</span></span><br><span class="line">	FR_NO_FILESYSTEM,		<span class="comment">/* (13) There is no valid FAT volume */</span></span><br><span class="line">	FR_MKFS_ABORTED,		<span class="comment">/* (14) The f_mkfs() aborted due to any problem */</span></span><br><span class="line">	FR_TIMEOUT,				<span class="comment">/* (15) Could not get a grant to access the volume within defined period */</span></span><br><span class="line">	FR_LOCKED,				<span class="comment">/* (16) The operation is rejected according to the file sharing policy */</span></span><br><span class="line">	FR_NOT_ENOUGH_CORE,		<span class="comment">/* (17) LFN working buffer could not be allocated */</span></span><br><span class="line">	FR_TOO_MANY_OPEN_FILES,	<span class="comment">/* (18) Number of open files &gt; FF_FS_LOCK */</span></span><br><span class="line">	FR_INVALID_PARAMETER	<span class="comment">/* (19) Given parameter is invalid */</span></span><br><span class="line">&#125; FRESULT;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这是一个枚举类型，它也定义在ff.h中，我们可以参考官方文档来详细了解这些值的含义：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/rc.html#id">FatFs - API Return Code (elm-chan.org)</a></p>
<h3 id="2-2-字符串数组？"><a href="#2-2-字符串数组？" class="headerlink" title="2.2 字符串数组？"></a><font size=3>2.2 字符串数组？</font></h3><p>上边返回值好多，我们知道值的话，还需要知道代表什么含义，我们其实可以定义一个二维数组，按顺序将上边的枚举所代表的含义依次写入，然后打印的时候就可以直接获取错误码的含义了，不过这样可能会比较占内存，慎用。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> BYTE API_RET[][<span class="number">90</span>] = &#123;</span><br><span class="line">    <span class="string">&quot;(0) Succeeded &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(1) A hard error occurred in the low level disk I/O layer &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(2) Assertion failed &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(3) The physical drive cannot work &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(4) Could not find the file &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(5) Could not find the path &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(6) The path name format is invalid &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(7) Access denied due to prohibited access or directory full &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(8) Access denied due to prohibited access &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(9) The file/directory object is invalid &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(10) The physical drive is write protected &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(11) The logical drive number is invalid &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(12) The volume has no work area &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(13) There is no valid FAT volume &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(14) The f_mkfs() aborted due to any problem &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(15) Could not get a grant to access the volume within defined period &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(16) The operation is rejected according to the file sharing policy &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(17) LFN working buffer could not be allocated &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(18) Number of open files &gt; FF_FS_LOCK &quot;</span>,</span><br><span class="line">    <span class="string">&quot;(19) Given parameter is invalid &quot;</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>我们打印的时候就可以这样：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;res:%s\n\r&quot;</span>, API_RET[res]);</span><br></pre></td></tr></table></figure>

<p>这样就可以直接将错误的原因打印出来啦。</p>
<h2 id="3-常用函数"><a href="#3-常用函数" class="headerlink" title="3. 常用函数"></a><font size=3>3. 常用函数</font></h2><h3 id="3-1-f-mount"><a href="#3-1-f-mount" class="headerlink" title="3.1 f_mount()"></a><font size=3>3.1 f_mount()</font></h3><p>我们可以参考<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/mount.html">FatFs - f_mount (elm-chan.org)</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">FRESULT <span class="title function_">f_mount</span> <span class="params">(</span></span><br><span class="line"><span class="params">  FATFS*       fs,    <span class="comment">/* [IN] Filesystem object */</span></span></span><br><span class="line"><span class="params">  <span class="type">const</span> TCHAR* path,  <span class="comment">/* [IN] Logical drive number */</span></span></span><br><span class="line"><span class="params">  BYTE         opt    <span class="comment">/* [IN] Initialization option */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>f_mount函数为FatFs模块提供工作区域。也就是用于挂载我们存储设备上的文件系统，挂载之后我们才能正常的使用这个文件系统。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li>fs ：指向要注册和清除的文件系统对象的指针。若是NULL的话，则表示取消注册已注册的文件系统对象，每个存储介质只需要一个即可，用于存储这个存储设备的文件系统的信息。</li>
<li>path ：指向指定逻辑驱动器的以空结尾的字符串的指针。不带驱动器号的字符串为默认驱动器。比如我们上边规定的SPI FLASH的drive number为1，那么这里就可以写 “1:” ，当挂载成功后，他就相当于我们win中的C、D等这些的盘符。</li>
<li>opt ：0，现在不挂载(要在第一次访问卷时挂载)，1，强制立即挂载卷，并检查卷是否可以工作。</li>
</ul>
<h3 id="3-2-f-mkfs"><a href="#3-2-f-mkfs" class="headerlink" title="3.2 f_mkfs()"></a><font size=3>3.2 f_mkfs()</font></h3><p>我们可以参考<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/mkfs.html">FatFs - f_mkfs (elm-chan.org)</a>:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">FRESULT <span class="title function_">f_mkfs</span> <span class="params">(</span></span><br><span class="line"><span class="params">  <span class="type">const</span> TCHAR* path,   <span class="comment">/* [IN] Logical drive number */</span></span></span><br><span class="line"><span class="params">  <span class="type">const</span> MKFS_PARM* opt,<span class="comment">/* [IN] Format options */</span></span></span><br><span class="line"><span class="params">  <span class="type">void</span>* work,          <span class="comment">/* [-]  Working buffer */</span></span></span><br><span class="line"><span class="params">  UINT len             <span class="comment">/* [IN] Size of working buffer */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>f_mkfs函数在逻辑驱动器上创建一个FAT&#x2F;exFAT卷。什么意思呢？就是我们的存储介质上原本是什么都没有的，我们需要将其格式化并创建一个文件系统，这样我们才能正常的去使用文件系统来管理其中的文件。</p>
<p><strong>【函数参数】</strong></p>
<ul>
<li><p>path：指向以空结尾的字符串的指针指定要格式化的逻辑驱动器。如果其中没有驱动器号，则表示指定默认驱动器。在格式化过程中，逻辑驱动器可能已经挂载，也可能没有挂载。</p>
</li>
<li><p>opt ：MKFS_PARM 类型结构体指针变量，表示格式选项。如果给出一个空指针，它将给函数提供默认值中的每个选项，详细情况我们可以看一下参考文档中的详细说明。</p>
</li>
<li><p>work ：指向格式化过程使用的工作缓冲区的指针。如果 null 指针以 FF_USE_LFN &#x3D;&#x3D; 3 给出，则该函数在此函数中使用 len 字节的堆内存。</p>
</li>
<li><p>len ：工作缓冲区的大小，以字节为单位。它至少需要是FF_MAX_SS。大量的工作缓冲区减少了对驱动器的写事务数量，因此格式化过程将很快完成。</p>
</li>
</ul>
<h2 id="4-时间函数get-fattime"><a href="#4-时间函数get-fattime" class="headerlink" title="4. 时间函数get_fattime()"></a><font size=3>4. 时间函数get_fattime()</font></h2><p>这个函数是所有存储介质移植的时候都需要的，这个函数在FatFs源码中没有定义，但是又用到了，所以还是需要定义一下的，不然会报错。我们可以看一下参考文档<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/fattime.html">FatFs - get_fattime (elm-chan.org)</a>，函数声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DWORD <span class="title function_">get_fattime</span> <span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p>这个函数主要是获取当前的时间，这个函数在FatFs系统中没有实现，我们直接编译是会报这个错误的，我们可以通过STM32的RTC来获取时间，当然，若是暂时不需要，我们可以将其定义为如下的形式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  获取当前系统时间</span></span><br><span class="line"><span class="comment">  * @note   用于文件时间属性的确定</span></span><br><span class="line"><span class="comment">  * @param  </span></span><br><span class="line"><span class="comment">  * @retval </span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">DWORD <span class="title function_">get_fattime</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 返回当前时间戳 */</span></span><br><span class="line">	<span class="keyword">return</span> ((DWORD)(<span class="number">2015</span> - <span class="number">1980</span>) &lt;&lt; <span class="number">25</span>) <span class="comment">/* Year 2015 */</span></span><br><span class="line">		   | ((DWORD)<span class="number">1</span> &lt;&lt; <span class="number">21</span>)			<span class="comment">/* Month 1 */</span></span><br><span class="line">		   | ((DWORD)<span class="number">1</span> &lt;&lt; <span class="number">16</span>)			<span class="comment">/* Mday 1 */</span></span><br><span class="line">		   | ((DWORD)<span class="number">0</span> &lt;&lt; <span class="number">11</span>)			<span class="comment">/* Hour 0 */</span></span><br><span class="line">		   | ((DWORD)<span class="number">0</span> &lt;&lt; <span class="number">5</span>)			<span class="comment">/* Min 0 */</span></span><br><span class="line">		   | ((DWORD)<span class="number">0</span> &gt;&gt; <span class="number">1</span>);			<span class="comment">/* Sec 0 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="二、基于SPI-FLASH的文件系统"><a href="#二、基于SPI-FLASH的文件系统" class="headerlink" title="二、基于SPI FLASH的文件系统"></a><font size=3>二、基于SPI FLASH的文件系统</font></h1><p>我们先来看一下以SPI FLASH为存储介质的FatFs文件系统的移植。</p>
<h2 id="1-底层读写"><a href="#1-底层读写" class="headerlink" title="1. 底层读写"></a><font size=3>1. 底层读写</font></h2><h3 id="1-1-初始化"><a href="#1-1-初始化" class="headerlink" title="1.1 初始化"></a><font size=3>1.1 初始化</font></h3><p>首先我们需要初始化SPI FLASH，在这里我使用的是W25Q128，由于我使用的是STM32CubeMX来配置的SPI，所以初始化就如下函数所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* SPI2 init function */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">MX_SPI2_Init</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<h3 id="1-2-获取SPI-FLASH器件ID"><a href="#1-2-获取SPI-FLASH器件ID" class="headerlink" title="1.2 获取SPI FLASH器件ID"></a><font size=3>1.2 获取SPI FLASH器件ID</font></h3><p>这一步其实本不是必须，但是由于代码支持了其他型号的SPI FLASH，所以这一步也就成了必要的步骤，其实也有好处，那就是可以通过读取ID来判断我们的SPI FLASH是否已经初始化完成：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  获取W25QXX型号</span></span><br><span class="line"><span class="comment">  * @note   目前只支持W25Q128</span></span><br><span class="line"><span class="comment">  * @param  </span></span><br><span class="line"><span class="comment">  * @retval 返回0表示W25Q128状态正常，返回-1表示W25Q128不可用</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">int8_t</span> <span class="title function_">Get_W25QXX_Type</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    W25QXX_TYPE = W25QXX_ReadID();</span><br><span class="line">    <span class="comment">//printf(&quot;SPI FLASH ID=%#x\r\n&quot;, W25QXX_TYPE);</span></span><br><span class="line">    <span class="keyword">if</span>(W25QXX_TYPE == <span class="number">0XEF17</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;W25QXX_TYPE not is W25Q128!!!\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3-读取数据"><a href="#1-3-读取数据" class="headerlink" title="1.3 读取数据"></a><font size=3>1.3 读取数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  读取SPI FLASH</span></span><br><span class="line"><span class="comment">  * @note   在指定地址开始读取指定长度的数据</span></span><br><span class="line"><span class="comment">  * @param  pBuffer 数据存储区</span></span><br><span class="line"><span class="comment">  * @param  ReadAddr 开始读取的地址(24bit)</span></span><br><span class="line"><span class="comment">  * @param  NumByteToRead 要读取的字节数(最大65535)</span></span><br><span class="line"><span class="comment">  * @retval 成功返回0</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">int8_t</span> <span class="title function_">W25QXX_Read</span><span class="params">(<span class="type">uint8_t</span> *pBuffer, <span class="type">uint32_t</span> ReadAddr, <span class="type">uint16_t</span> NumByteToRead)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint16_t</span> i;</span><br><span class="line">    W25QXX_CS = <span class="number">0</span>;                     <span class="comment">// 使能器件</span></span><br><span class="line">    SPI2_ReadWriteByte(W25X_ReadData); <span class="comment">// 发送读取命令</span></span><br><span class="line">    <span class="keyword">if</span> (W25QXX_TYPE == W25Q256)        <span class="comment">// 如果是W25Q256的话地址为4字节的，要发送最高8位</span></span><br><span class="line">    &#123;</span><br><span class="line">        SPI2_ReadWriteByte((<span class="type">uint8_t</span>)((ReadAddr) &gt;&gt; <span class="number">24</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    SPI2_ReadWriteByte((<span class="type">uint8_t</span>)((ReadAddr) &gt;&gt; <span class="number">16</span>)); <span class="comment">// 发送24bit地址</span></span><br><span class="line">    SPI2_ReadWriteByte((<span class="type">uint8_t</span>)((ReadAddr) &gt;&gt; <span class="number">8</span>));</span><br><span class="line">    SPI2_ReadWriteByte((<span class="type">uint8_t</span>)ReadAddr);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NumByteToRead; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        pBuffer[i] = SPI2_ReadWriteByte(<span class="number">0XFF</span>); <span class="comment">// 循环读数</span></span><br><span class="line">    &#125;</span><br><span class="line">    W25QXX_CS = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-4-写入数据"><a href="#1-4-写入数据" class="headerlink" title="1.4 写入数据"></a><font size=3>1.4 写入数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 动态管理内存</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span>	SRAMIN   </span></span><br><span class="line"><span class="type">uint8_t</span> W25QXX_BUFFER[<span class="number">4096</span>];</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span>	</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  向 SPI FLASH 写入指定字节数据</span></span><br><span class="line"><span class="comment">  * @note   在指定地址开始写入指定长度的数据,该函数带擦除操作!</span></span><br><span class="line"><span class="comment">  * @param  pBuffer 数据存储区</span></span><br><span class="line"><span class="comment">  * @param  WriteAddr 开始写入的地址(24bit)</span></span><br><span class="line"><span class="comment">  * @param  NumByteToWrite 要写入的字节数(最大65535)  </span></span><br><span class="line"><span class="comment">  * @retval 成功返回0</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">int8_t</span> <span class="title function_">W25QXX_Write</span><span class="params">(<span class="type">uint8_t</span> *pBuffer, <span class="type">uint32_t</span> WriteAddr, <span class="type">uint16_t</span> NumByteToWrite)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint32_t</span> secpos;</span><br><span class="line">    <span class="type">uint16_t</span> secoff;</span><br><span class="line">    <span class="type">uint16_t</span> secremain;</span><br><span class="line">    <span class="type">uint16_t</span> i;</span><br><span class="line">    <span class="type">uint8_t</span> *W25QXX_BUF;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> SRAMIN</span></span><br><span class="line">    W25QXX_BUF = (<span class="type">uint8_t</span> *)pub_malloc(SRAMIN, <span class="number">4096</span>); <span class="comment">// 申请内存</span></span><br><span class="line">    <span class="keyword">if</span> (W25QXX_BUF == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        PRTE(<span class="string">&quot;pub_malloc failed!!!\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>; <span class="comment">// 申请失败</span></span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">    W25QXX_BUF = W25QXX_BUFFER;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    secpos = WriteAddr / W25QXX_SECTOR_SIZE; <span class="comment">// 扇区地址</span></span><br><span class="line">    secoff = WriteAddr % W25QXX_SECTOR_SIZE; <span class="comment">// 在扇区内的偏移</span></span><br><span class="line">    secremain = W25QXX_SECTOR_SIZE - secoff; <span class="comment">// 扇区剩余空间大小</span></span><br><span class="line">    <span class="comment">// printf(&quot;ad:%X,nb:%X\r\n&quot;,WriteAddr,NumByteToWrite);//测试用</span></span><br><span class="line">    <span class="keyword">if</span> (NumByteToWrite &lt;= secremain)</span><br><span class="line">        secremain = NumByteToWrite; <span class="comment">// 不大于4096个字节</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        W25QXX_Read(W25QXX_BUF, secpos * W25QXX_SECTOR_SIZE, W25QXX_SECTOR_SIZE); <span class="comment">// 读出整个扇区的内容</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; secremain; i++)                                           <span class="comment">// 校验数据</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (W25QXX_BUF[secoff + i] != <span class="number">0XFF</span>)</span><br><span class="line">                <span class="keyword">break</span>; <span class="comment">// 需要擦除</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; secremain) <span class="comment">// 需要擦除</span></span><br><span class="line">        &#123;</span><br><span class="line">            W25QXX_Erase_Sector(secpos);    <span class="comment">// 擦除这个扇区</span></span><br><span class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; secremain; i++) <span class="comment">// 复制</span></span><br><span class="line">            &#123;</span><br><span class="line">                W25QXX_BUF[i + secoff] = pBuffer[i];</span><br><span class="line">            &#125;</span><br><span class="line">            W25QXX_Write_NoCheck(W25QXX_BUF, secpos * W25QXX_SECTOR_SIZE, W25QXX_SECTOR_SIZE); <span class="comment">// 写入整个扇区</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            W25QXX_Write_NoCheck(pBuffer, WriteAddr, secremain); <span class="comment">// 写已经擦除了的,直接写入扇区剩余区间.</span></span><br><span class="line">        <span class="keyword">if</span> (NumByteToWrite == secremain)</span><br><span class="line">            <span class="keyword">break</span>; <span class="comment">// 写入结束了</span></span><br><span class="line">        <span class="keyword">else</span>       <span class="comment">// 写入未结束</span></span><br><span class="line">        &#123;</span><br><span class="line">            secpos++;   <span class="comment">// 扇区地址增1</span></span><br><span class="line">            secoff = <span class="number">0</span>; <span class="comment">// 偏移位置为0</span></span><br><span class="line"></span><br><span class="line">            pBuffer += secremain;        <span class="comment">// 指针偏移</span></span><br><span class="line">            WriteAddr += secremain;      <span class="comment">// 写地址偏移</span></span><br><span class="line">            NumByteToWrite -= secremain; <span class="comment">// 字节数递减</span></span><br><span class="line">            <span class="keyword">if</span> (NumByteToWrite &gt; W25QXX_SECTOR_SIZE)</span><br><span class="line">                secremain = W25QXX_SECTOR_SIZE; <span class="comment">// 下一个扇区还是写不完</span></span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                secremain = NumByteToWrite; <span class="comment">// 下一个扇区可以写完了</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> SRAMIN</span></span><br><span class="line">    pub_free(SRAMIN, W25QXX_BUF); <span class="comment">// 释放内存</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-diskio-c文件的移植"><a href="#2-diskio-c文件的移植" class="headerlink" title="2. diskio.c文件的移植"></a><font size=3>2. diskio.c文件的移植</font></h2><p>接下来，就来一步一步移植这个文件系统，然后看一看中间可能会有什么坑。这一部分主要都是在diskio.c文件中进行移植，这里先不关心配置文件，后边测试的时候会根据出错的情况一步一步修改配置文件。</p>
<h3 id="2-1-drive-number"><a href="#2-1-drive-number" class="headerlink" title="2.1 drive number"></a><font size=3>2.1 drive number</font></h3><p>首先，我们需要定义我们的盘符，在 diskio.c 文件的开头有以下几个宏：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Definitions of physical drive number for each drive */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_RAM		0	<span class="comment">/* Example: Map Ramdisk to physical drive 0 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_MMC		1	<span class="comment">/* Example: Map MMC/SD card to physical drive 1 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_USB		2	<span class="comment">/* Example: Map USB MSD to physical drive 2 */</span></span></span><br></pre></td></tr></table></figure>

<p>这些宏就表示了不同的存储介质，后边会根据这些宏来调用不同的初始化及读写函数去操作对应的存储设备。我们这里定义两个，一个是SD卡（预留，后边会使用），一个是SPI FLASH：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_SDCARD      0	<span class="comment">/* Example: Map SD card to physical drive 0 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_SPI_FLASH   1	<span class="comment">/* Example: Map SPI FLASH to physical drive 1 */</span></span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-disk-initialize"><a href="#2-2-disk-initialize" class="headerlink" title="2.2 disk_initialize()"></a><font size=3>2.2 disk_initialize()</font></h3><h4 id="2-2-1-函数声明"><a href="#2-2-1-函数声明" class="headerlink" title="2.2.1 函数声明"></a><font size=3>2.2.1 函数声明</font></h4><p>首先是初始化函数disk_initialize()，我们可以参考文档<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dinit.html">FatFs - disk_initialize (elm-chan.org)</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_initialize</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv           <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p>这个函数初始化存储设备，并使其准备好进行通用读&#x2F;写。</p>
<h4 id="2-2-2-对SPI-FLASH初始化"><a href="#2-2-2-对SPI-FLASH初始化" class="headerlink" title="2.2.2 对SPI FLASH初始化"></a><font size=3>2.2.2 对SPI FLASH初始化</font></h4><p>我们来看一下对SPI FLASH的初始化操作：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">    <span class="comment">//MX_SPI2_Init(); // SPI初始化，在main.c中完成 </span></span><br><span class="line">    result = Get_W25QXX_Type();<span class="comment">// 这里最开始的时候写的是 result = 0; 但是这样是无法完整对SPI FLASH初始化，后边会分析问题所在。</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<p>由于我使用的是STM32CubeMX来配置的SPI，所以当STM32CubeMX生成工程的时候，会直接在main函数中进行SPI初始化函数的调用，所以为了后边修改工程方便，这里就不再放对SPI的初始化了。</p>
<h4 id="2-2-3-移植结果"><a href="#2-2-3-移植结果" class="headerlink" title="2.2.3 移植结果"></a><font size=3>2.2.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_initialize</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">			<span class="comment">//MX_SPI2_Init(); // SPI初始化，在main.c中完成</span></span><br><span class="line">			result = Get_W25QXX_Type();</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> STA_NOINIT; <span class="comment">//初始化失败</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-disk-status"><a href="#2-3-disk-status" class="headerlink" title="2.3 disk_status()"></a><font size=3>2.3 disk_status()</font></h3><h4 id="2-3-1-函数声明"><a href="#2-3-1-函数声明" class="headerlink" title="2.3.1 函数声明"></a><font size=3>2.3.1 函数声明</font></h4><p>这个函数主要是获取设备的状态，其实这个函数并不重要，我们甚至可以让它永远返回一个成功测标志。我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dstat.html">FatFs - disk_status (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_status</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv     <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-3-2-对SPI-FLASH的状态检测"><a href="#2-3-2-对SPI-FLASH的状态检测" class="headerlink" title="2.3.2 对SPI FLASH的状态检测"></a><font size=3>2.3.2 对SPI FLASH的状态检测</font></h4><p>我们可以将获取W25Q128器件ID的操作放在状态读取的函数中，这样也可以确保我们的SPI FLASH可以正常运行：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">    result = Get_W25QXX_Type();</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-3-3-移植结果"><a href="#2-3-3-移植结果" class="headerlink" title="2.3.3 移植结果"></a><font size=3>2.3.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_status</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">			result = Get_W25QXX_Type();</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> STA_NOINIT;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-4-disk-read"><a href="#2-4-disk-read" class="headerlink" title="2.4 disk_read()"></a><font size=3>2.4 disk_read()</font></h3><h4 id="2-4-1-函数声明"><a href="#2-4-1-函数声明" class="headerlink" title="2.4.1 函数声明"></a><font size=3>2.4.1 函数声明</font></h4><p>这个函数主要是读取数据，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dread.html">FatFs - disk_read (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_read</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,     <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">  BYTE* buff,    <span class="comment">/* [OUT] Pointer to the read data buffer */</span></span></span><br><span class="line"><span class="params">  LBA_t sector,  <span class="comment">/* [IN] Start sector number */</span></span></span><br><span class="line"><span class="params">  UINT count     <span class="comment">/* [IN] Number of sectros to read */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>buff ：指向字节数组中用于存储读取数据的第一个数据的指针。读取数据的大小将是 扇区大小x字节数。</p>
</li>
<li><p>sector ：启动扇区号。数据类型 LBA_t 是 DWORD 或 QWORD 的别名，具体取决于配置选项。</p>
</li>
<li><p>count ：要读取的扇区数。</p>
</li>
</ul>
<p><strong>【注意事项】</strong>FatFs每次操作，都是以扇区为基本单位的。</p>
<h4 id="2-4-2-从SPI-FLASH读取"><a href="#2-4-2-从SPI-FLASH读取" class="headerlink" title="2.4.2 从SPI FLASH读取"></a><font size=3>2.4.2 从SPI FLASH读取</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">    result = W25QXX_Read((<span class="type">uint8_t</span> *)buff, sector * <span class="number">4096</span>, count * <span class="number">4096</span>);</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<p>前边我们知道，W25Q128将16MB的容量分为256个块（Block），每个块大小为64K字节，每个块又分为16个扇区（Sector），每个扇区4K个字节。而我们操作W25Q128的时候每次也是擦除一个扇区，也就是4KB，16M的空间，一共就有4096个扇区，而扇区号乘以扇区大小就可以得到整个扇区的起始地址。count 表示扇区的数量，每个扇区4KB，相乘就可以得到要读取的字节数量。</p>
<h4 id="2-4-3-移植结果"><a href="#2-4-3-移植结果" class="headerlink" title="2.4.3 移植结果"></a><font size=3>2.4.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_read</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv,	  <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">	BYTE *buff,	  <span class="comment">/* Data buffer to store read data */</span></span></span><br><span class="line"><span class="params">	LBA_t sector, <span class="comment">/* Start sector in LBA */</span></span></span><br><span class="line"><span class="params">	UINT count	  <span class="comment">/* Number of sectors to read */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">			result = W25QXX_Read((<span class="type">uint8_t</span> *)buff, sector * <span class="number">4096</span>, count * <span class="number">4096</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-5-disk-write"><a href="#2-5-disk-write" class="headerlink" title="2.5 disk_write()"></a><font size=3>2.5 disk_write()</font></h3><h4 id="2-5-1-函数声明"><a href="#2-5-1-函数声明" class="headerlink" title="2.5.1 函数声明"></a><font size=3>2.5.1 函数声明</font></h4><p>这个函数主要是写入数据，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dwrite.html">FatFs - disk_write (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_write</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,        <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">  <span class="type">const</span> BYTE* buff, <span class="comment">/* [IN] Pointer to the data to be written */</span></span></span><br><span class="line"><span class="params">  LBA_t sector,     <span class="comment">/* [IN] Sector number to write from */</span></span></span><br><span class="line"><span class="params">  UINT count        <span class="comment">/* [IN] Number of sectors to write */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>buff ：指向字节数组中用于存储写入数据的第一个数据的指针。写入数据的大小将是 扇区大小x字节数。</p>
</li>
<li><p>sector ：启动扇区号。数据类型 LBA_t 是 DWORD 或 QWORD 的别名，具体取决于配置选项。</p>
</li>
<li><p>count ：要读取的扇区数。</p>
</li>
</ul>
<p><strong>【注意事项】</strong>FatFs每次操作，都是以扇区为基本单位的。</p>
<h4 id="2-5-2-向SPI-FLASH写入"><a href="#2-5-2-向SPI-FLASH写入" class="headerlink" title="2.5.2 向SPI FLASH写入"></a><font size=3>2.5.2 向SPI FLASH写入</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">    result = W25QXX_Write((<span class="type">uint8_t</span> *)buff, sector * <span class="number">4096</span>, count * <span class="number">4096</span>);</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<p>这里与前边的读一样，都是以扇区为单位操作，将参数传入底层写入函数的时候需要转换为相应的地址。</p>
<h4 id="2-5-3-移植结果"><a href="#2-5-3-移植结果" class="headerlink" title="2.5.3 移植结果"></a><font size=3>2.5.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_write</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv,		  <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">	<span class="type">const</span> BYTE *buff, <span class="comment">/* Data to be written */</span></span></span><br><span class="line"><span class="params">	LBA_t sector,	  <span class="comment">/* Start sector in LBA */</span></span></span><br><span class="line"><span class="params">	UINT count		  <span class="comment">/* Number of sectors to write */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">			result = W25QXX_Write((<span class="type">uint8_t</span> *)buff, sector * <span class="number">4096</span>, count * <span class="number">4096</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-6-disk-ioctl"><a href="#2-6-disk-ioctl" class="headerlink" title="2.6 disk_ioctl()"></a><font size=3>2.6 disk_ioctl()</font></h3><h4 id="2-6-1-函数声明"><a href="#2-6-1-函数声明" class="headerlink" title="2.6.1 函数声明"></a><font size=3>2.6.1 函数声明</font></h4><p>这个函数主要是用于控制设备，可根据不同命令执行不同功能，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dioctl.html">FatFs - disk_ioctl (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_ioctl</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,     <span class="comment">/* [IN] Drive number */</span></span></span><br><span class="line"><span class="params">  BYTE cmd,      <span class="comment">/* [IN] Control command code */</span></span></span><br><span class="line"><span class="params">  <span class="type">void</span>* buff     <span class="comment">/* [I/O] Parameter and data buffer */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>cmd ：命令编号，比如GET_SECTOR_COUNT、GET_SECTOR_SIZE、GET_BLOCK_SIZE等，详细的命令及含义可以参考官方文档说明。</p>
</li>
<li><p>buff ：参数的指针取决于命令代码。不要在意该命令是否没有要传递的参数。</p>
</li>
</ul>
<h4 id="2-6-2-获取SPI-FLASH信息"><a href="#2-6-2-获取SPI-FLASH信息" class="headerlink" title="2.6.2 获取SPI FLASH信息"></a><font size=3>2.6.2 获取SPI FLASH信息</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">    <span class="keyword">switch</span>(cmd)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">case</span> GET_SECTOR_COUNT:<span class="comment">// 扇区数量</span></span><br><span class="line">            *(DWORD *)buff = <span class="number">256</span> * <span class="number">16</span>;<span class="comment">//256块x16个扇区</span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GET_SECTOR_SIZE:<span class="comment">// 扇区大小</span></span><br><span class="line">            *(WORD *)buff = <span class="number">4096</span>;<span class="comment">// 每个扇区是4KB</span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GET_BLOCK_SIZE:<span class="comment">// 每次擦除块的大小的个数</span></span><br><span class="line">            *(DWORD *)buff = <span class="number">1</span>;<span class="comment">// 每次只擦除一个扇区</span></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    result = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-6-3-移植结果"><a href="#2-6-3-移植结果" class="headerlink" title="2.6.3 移植结果"></a><font size=3>2.6.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_ioctl</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv, <span class="comment">/* Physical drive nmuber (0..) */</span></span></span><br><span class="line"><span class="params">	BYTE cmd,  <span class="comment">/* Control code */</span></span></span><br><span class="line"><span class="params">	<span class="type">void</span> *buff <span class="comment">/* Buffer to send/receive control data */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SPI_FLASH:</span><br><span class="line">			<span class="keyword">switch</span>(cmd)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">case</span> GET_SECTOR_COUNT:<span class="comment">// 扇区数量</span></span><br><span class="line">					*(DWORD *)buff = <span class="number">256</span> * <span class="number">16</span>;<span class="comment">//256块x16个扇区</span></span><br><span class="line">					<span class="comment">//*(DWORD *)buff = FLASH_SECTOR_COUNT;//12M/512B个扇区</span></span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				<span class="keyword">case</span> GET_SECTOR_SIZE:<span class="comment">// 扇区大小</span></span><br><span class="line">					*(WORD *)buff = <span class="number">4096</span>;<span class="comment">// 每个扇区是4KB</span></span><br><span class="line">					<span class="comment">//*(WORD *)buff = FLASH_SECTOR_SIZE;// 每个扇区是512B</span></span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				<span class="keyword">case</span> GET_BLOCK_SIZE:<span class="comment">// 每次擦除块的大小的个数</span></span><br><span class="line">					*(DWORD *)buff = <span class="number">1</span>;<span class="comment">// 每次只擦除一个扇区</span></span><br><span class="line">					<span class="comment">//*(DWORD *)buff = FLASH_BLOCK_SIZE;// 每次只擦除一个大扇区，512*8=4096</span></span><br><span class="line">				<span class="keyword">default</span>:</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			result = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-ffconf-h文件的配置"><a href="#3-ffconf-h文件的配置" class="headerlink" title="3. ffconf.h文件的配置"></a><font size=3>3. ffconf.h文件的配置</font></h2><p>这一节我们通过报错来看一下不同的配置项有什么影响。我们这部分以例子来说明。</p>
<h3 id="3-1-FF-VOLUMES"><a href="#3-1-FF-VOLUMES" class="headerlink" title="3.1 FF_VOLUMES"></a><font size=3>3.1 FF_VOLUMES</font></h3><p>测试函数我们用f_mount来尝试：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> FATFS fs = &#123;<span class="number">0</span>&#125;; <span class="comment">// FatFs文件系统对象</span></span><br><span class="line"><span class="type">static</span> BYTE work[FF_MAX_SS];  <span class="comment">// 格式化设备工作区</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">fatfs_Test</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    FRESULT res = FR_OK;</span><br><span class="line">	res = f_mount(&amp;fs, <span class="string">&quot;1:&quot;</span>, <span class="number">1</span>); <span class="comment">// 挂载FLASH.</span></span><br><span class="line">	<span class="keyword">if</span>(res != FR_OK)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;f_mount error:%s\r\n&quot;</span>, API_RET[res]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们调用并打印，会有如下打印：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">f_mount error:(<span class="number">11</span>) The logical drive number is invalid</span><br></pre></td></tr></table></figure>

<p>这是怎么回事？我们来看一下，报错写的是我们的逻辑驱动器编号不合法，我们看一下我们定义的drive number，发现SPI FLASH是1，这没问题啊，我们从f_mount()函数一级一级往下找，发现我们的驱动器编号收到这个宏的限制 FF_VOLUMES，它表示我们的文件系统最多支持多少个驱动器，也就是存储设备，我们这个宏默认是1，那么我们挂载的时候就只能挂载 “0:” ，我们修改宏为2：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_VOLUMES		2</span></span><br><span class="line"><span class="comment">/* Number of volumes (logical drives) to be used. (1-10) */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-FF-MAX-SS"><a href="#3-2-FF-MAX-SS" class="headerlink" title="3.2 FF_MAX_SS"></a><font size=3>3.2 FF_MAX_SS</font></h3><p>不出意外的话上边宏改完，就不会报11这个错了，但是我们会进入另一个错误：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[error]HardFault!!!</span><br></pre></td></tr></table></figure>

<p>这是什么？这是我们之前添加的硬件错误中断中打印的，怎么会发生这样一个错误？一般来讲都是内存溢出，或者访问了空的指针才会出现。我们还记得上边的FATFS结构体吧，里边有一个成员 win[FF_MAX_SS]，我们看一下这个宏默认是512，所以这是一个512字节的数组，由于我们定义的fs是个全局变量，所以栈空间应该不会溢出，那怎么回事呢？我们现在用的SPI FLASH一个扇区是4096字节，并且我们在disk_ioctl()函数中已经设置了一个扇区为4096，若是初始化的时候操作SPI FLASH用到这个扇区大小的话，可能就溢出了，这也很有可能就会导致上边的错误，我们来看一下函数调用关系（Source Insight）：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/image-20230602224918758.png" alt="image-20230602224918758" style="zoom:50%;" />

<p>发现f_mount()函数挂载会使用到这个成员，那么基本就可以验证我们的猜想了，所以我们需要将此处改为4096：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_MIN_SS		512</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> FF_MAX_SS		4096</span></span><br><span class="line"><span class="comment">/* This set of options configures the range of sector size to be supported. (512,</span></span><br><span class="line"><span class="comment">/  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and</span></span><br><span class="line"><span class="comment">/  harddisk, but a larger value may be required for on-board flash memory and some</span></span><br><span class="line"><span class="comment">/  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured</span></span><br><span class="line"><span class="comment">/  for variable sector size mode and disk_ioctl() function needs to implement</span></span><br><span class="line"><span class="comment">/  GET_SECTOR_SIZE command. */</span></span><br></pre></td></tr></table></figure>

<p>然后我们再编译下载，就会产生其他错误了，就不会再是这个硬件出错误了：</p>
<h3 id="3-3-FF-USE-MKFS"><a href="#3-3-FF-USE-MKFS" class="headerlink" title="3.3 FF_USE_MKFS"></a><font size=3>3.3 FF_USE_MKFS</font></h3><p>新的bug：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">f_mount error:(<span class="number">13</span>) There is no valid FAT volume</span><br></pre></td></tr></table></figure>

<p>这又是什么？There is no valid FAT volume翻译过来就是没有有效的FAT卷，也就是没有文件系统，这样就可以理解了，原本我们的SPI FLASH就什么都没有。那么现在我们就需要格式化SPI FLASH 来创建一个文件系统，这个时候就需要用到f_mkfs()函数啦，我们修改测试函数如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> FATFS fs = &#123;<span class="number">0</span>&#125;; <span class="comment">// FatFs文件系统对象</span></span><br><span class="line"><span class="type">static</span> BYTE work[FF_MAX_SS];  <span class="comment">// 格式化设备工作区</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">fatfs_Test</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    FRESULT res = FR_OK;</span><br><span class="line">	res = f_mount(&amp;fs, <span class="string">&quot;1:&quot;</span>, <span class="number">1</span>); <span class="comment">// 挂载FLASH.</span></span><br><span class="line">	<span class="keyword">if</span>(res != FR_OK)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;f_mount error:%s\r\n&quot;</span>, API_RET[res]);</span><br><span class="line">        <span class="keyword">if</span> (res == FR_NO_FILESYSTEM) <span class="comment">// FLASH磁盘,FAT文件系统错误,重新格式化FLASH</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;Flash Disk Formatting...\r\n&quot;</span>); <span class="comment">// 格式化FLASH</span></span><br><span class="line"></span><br><span class="line">            res = f_mkfs(<span class="string">&quot;1:&quot;</span>, <span class="number">0</span>, work, <span class="keyword">sizeof</span>(work)); <span class="comment">// 格式化FLASH,1,盘符;1,不需要引导区,8个扇区为1个簇</span></span><br><span class="line">            <span class="keyword">if</span> (res == FR_OK)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;f_mkfs:Flash Disk Format Finish!!!\r\n&quot;</span>); <span class="comment">// 格式化完成</span></span><br><span class="line">                res = f_mount(<span class="literal">NULL</span>, <span class="string">&quot;1:&quot;</span>, <span class="number">1</span>);              <span class="comment">// 格式化后，先取消挂载</span></span><br><span class="line">                res = f_mount(&amp;fs, <span class="string">&quot;1:&quot;</span>, <span class="number">1</span>);               <span class="comment">// 重新挂载</span></span><br><span class="line">                <span class="keyword">if</span>(res != FR_OK)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="built_in">printf</span>(<span class="string">&quot;f_mount error:%s\r\n&quot;</span>, API_RET[res]);</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;f_mkfs error:%s\r\n&quot;</span>, API_RET[res]); <span class="comment">// 格式化失败</span></span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后我们发现编译直接报错：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Error: L6218E: Undefined symbol <span class="title function_">f_mkfs</span> <span class="params">(referred from fatfs_ex.o)</span>.</span><br></pre></td></tr></table></figure>

<p>发现没有定义这个符号，也就是没有这个函数，我们找一下这个函数，会发现，它受到这个宏的影响：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-22-FatFs-02-FatFs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%A7%BB%E6%A4%8D/img/image-20230602230210319.png" alt="image-20230602230210319" style="zoom:50%;" />

<p>我们要向使用这个宏，不能是只读的文件系统，并且还要开启FF_USE_MKFS，FF_FS_READONLY默认就是0，所以不用管。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_FS_READONLY  0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> FF_USE_MKFS		1</span></span><br><span class="line"><span class="comment">/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-4-SPI-FLASH的一个BUG"><a href="#3-4-SPI-FLASH的一个BUG" class="headerlink" title="3.4 SPI FLASH的一个BUG"></a><font size=3>3.4 SPI FLASH的一个BUG</font></h3><p>然后我们重新编译下载运行，发现我们重新挂载的时候又报错了：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">f_mount error:(<span class="number">13</span>) There is no valid FAT volume </span><br><span class="line">Flash Disk Formatting...</span><br><span class="line">f_mkfs:Flash Disk Format Finish!!!</span><br><span class="line">f_mount error:(<span class="number">13</span>) There is no valid FAT volume </span><br></pre></td></tr></table></figure>

<p>上边明明显示我们格式化完成了，但是依然没有成功创建文件系统，网上搜索了资料，配置文件到这里其实就可以了，我们再检查一下是不是写被禁止了，但是上边我们的文件系统根本就不是只读的，那么就有可能是我们底层接口问题了，还记得我们使用SPI FLASH的时候是要先擦除才能写入的，但是我们初始化函数中似乎并没有进行擦除，那么有又怎么把文件系统创建上去呢？所以我们在初始化的时候对扇区进行一次擦除，但是我们的写函数中已经内置了擦除功能了，那问题出在哪？</p>
<p>我后来直接在fatfs_Test()函数调用之前就读写一次SPI FLASH，发现之后就正常了，那么还是我们初始化的问题，问题就出现在我们的Get_W25QXX_Type()函数中，由于我们底层的SPI FLASH还可以支持其他类型的SPI FLASH，所以在使用之前是需要先获取ID的，但是我把这个函数放在了disk_status()中吗，就导致初始化有问题了，这个函数既可以用来确认SPI FLASH的状态，也是初始化所必要的所以我们需要修改一下 disk_initialize() 中对SPI FLASH的初始化，调用一次Get_W25QXX_Type()函数即可，然后再编译，下载运行就会发现，我们可以正常格式化以及挂载文件系统了：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SPI FLASH ID=<span class="number">0xef17</span></span><br><span class="line">f_mount success!!!</span><br></pre></td></tr></table></figure>



<h1 id="三、基于SD卡的文件系统"><a href="#三、基于SD卡的文件系统" class="headerlink" title="三、基于SD卡的文件系统"></a><font size=3>三、基于SD卡的文件系统</font></h1><p>我们先来看一下以SD卡为存储介质的FatFs文件系统的移植。另外就是，我后来移植的过程中，发现我们在使用STM32CubeMX配置SDIO的时候不要开SDIO和DMA中断，不然SD卡读写是没问题的，但是吧，移植好文件系统后，不知道为什么，怎么都跑不起来，具体是哪的影响还不清楚，后边知道了再补充。</p>
<h2 id="1-底层读写-1"><a href="#1-底层读写-1" class="headerlink" title="1. 底层读写"></a><font size=3>1. 底层读写</font></h2><h3 id="1-1-初始化-1"><a href="#1-1-初始化-1" class="headerlink" title="1.1 初始化"></a><font size=3>1.1 初始化</font></h3><p>首先我们需要初始化SD卡，这里还是使用STM32CubeMX来配置的SDIO，所以初始化就如下函数所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">MX_SDIO_SD_Init</span><span class="params">(<span class="type">void</span>)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>这个函数是完成SDIO的初始化，也就完成了对SD卡的初始化。注意这里有两个坑，一个就是初始化的时候的总线宽度设置为1位，还有就是时钟频率，若通信失败，可以降低试一试。这个里边要注意时钟</p>
<h3 id="1-2-获取SD卡信息"><a href="#1-2-获取SD卡信息" class="headerlink" title="1.2 获取SD卡信息"></a><font size=3>1.2 获取SD卡信息</font></h3><p>这一步主要是为了看一下SD卡是否初始化成功，并且获取一些SD卡的信息，后边使用：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">HAL_SD_CardInfoTypeDef  SDCardInfo;                 <span class="comment">//SD卡信息结构体</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">show_sdcard_info</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">uint64_t</span> CardCap;	<span class="comment">//SD卡容量</span></span><br><span class="line">	HAL_SD_CardCIDTypeDef SDCard_CID;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 检测SD卡是否正常（处于数据传输模式的传输状态） */</span></span><br><span class="line">    <span class="keyword">if</span>(HAL_SD_GetCardState(&amp;hsd) != HAL_SD_CARD_TRANSFER)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;SD card init fail!\r\n&quot;</span> );</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Initialize SD card successfully!\r\n&quot;</span>);</span><br><span class="line">	HAL_SD_GetCardCID(&amp;hsd, &amp;SDCard_CID);	<span class="comment">//获取CID</span></span><br><span class="line">	HAL_SD_GetCardInfo(&amp;hsd, &amp;SDCardInfo);  <span class="comment">//获取SD卡信息</span></span><br><span class="line">	<span class="keyword">switch</span>(SDCardInfo.CardType)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> CARD_SDSC:</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(SDCardInfo.CardVersion == CARD_V1_X)</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;Card Type          :SDSC V1 \r\n&quot;</span>);</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span>(SDCardInfo.CardVersion == CARD_V2_X)</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;Card Type          :SDSC V2 \r\n&quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> CARD_SDHC_SDXC:</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;Card Type          :SDHC \r\n&quot;</span>);</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;	</span><br><span class="line">	CardCap=(<span class="type">uint64_t</span>)(SDCardInfo.LogBlockNbr)*(<span class="type">uint64_t</span>)(SDCardInfo.LogBlockSize);	<span class="comment">//计算SD卡容量</span></span><br><span class="line">  	<span class="built_in">printf</span>(<span class="string">&quot;Card ManufacturerID:%d \r\n&quot;</span>,SDCard_CID.ManufacturerID);				<span class="comment">//制造商ID</span></span><br><span class="line"> 	<span class="built_in">printf</span>(<span class="string">&quot;Card RCA           :%d \r\n&quot;</span>,SDCardInfo.RelCardAdd);					<span class="comment">//卡相对地址</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;LogBlockNbr        :%d \r\n&quot;</span>,(<span class="type">uint32_t</span>)(SDCardInfo.LogBlockNbr));		<span class="comment">//显示逻辑块数量</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;LogBlockSize       :%d \r\n&quot;</span>,(<span class="type">uint32_t</span>)(SDCardInfo.LogBlockSize));		<span class="comment">//显示逻辑块大小</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;Card Capacity      :%d MB\r\n&quot;</span>,(<span class="type">uint32_t</span>)(CardCap&gt;&gt;<span class="number">20</span>));				<span class="comment">//显示容量</span></span><br><span class="line"> 	<span class="built_in">printf</span>(<span class="string">&quot;Card BlockSize     :%d \r\n\r\n&quot;</span>,SDCardInfo.BlockSize);					<span class="comment">//显示块大小</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3-读取数据-1"><a href="#1-3-读取数据-1" class="headerlink" title="1.3 读取数据"></a><font size=3>1.3 读取数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  从SD卡读取数据</span></span><br><span class="line"><span class="comment">  * @note   </span></span><br><span class="line"><span class="comment">  * @param  buf 读数据缓存区</span></span><br><span class="line"><span class="comment">  * @param  sector 扇区地址</span></span><br><span class="line"><span class="comment">  * @param  cnt 扇区个数</span></span><br><span class="line"><span class="comment">  * @retval 返回错误状态;0,正常;其他,错误代码;</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">uint8_t</span> <span class="title function_">SD_ReadDisk</span><span class="params">(<span class="type">uint8_t</span> *buf, <span class="type">uint32_t</span> sector, <span class="type">uint32_t</span> cnt)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> sta = HAL_OK;</span><br><span class="line">    <span class="type">uint32_t</span> timeout = SD_TIMEOUT;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> lsector = sector;</span><br><span class="line">    INTX_DISABLE(); <span class="comment">// 关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)</span></span><br><span class="line">    sta = HAL_SD_ReadBlocks(&amp;hsd, (<span class="type">uint8_t</span> *)buf, lsector, cnt, SD_TIMEOUT); <span class="comment">// 多个sector的读操作</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 等待SD卡读完</span></span><br><span class="line">    <span class="keyword">while</span> (SD_GetCardState() != SD_TRANSFER_OK)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (timeout-- == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            sta = SD_TRANSFER_BUSY;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    INTX_ENABLE(); <span class="comment">// 开启总中断</span></span><br><span class="line">    <span class="keyword">return</span> sta;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>在指定地址开始读取指定长度的数据。</p>
<p><strong>【参数说明】</strong></p>
<ul>
<li>buf ：读数据缓存区</li>
<li>sector ：扇区地址</li>
<li>cnt ：扇区个数</li>
</ul>
<p>	</p>
<h3 id="1-4-写入数据-1"><a href="#1-4-写入数据-1" class="headerlink" title="1.4 写入数据"></a><font size=3>1.4 写入数据</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  向SD卡写入数据</span></span><br><span class="line"><span class="comment">  * @note   </span></span><br><span class="line"><span class="comment">  * @param  buf 写数据缓存区</span></span><br><span class="line"><span class="comment">  * @param  sector 扇区地址</span></span><br><span class="line"><span class="comment">  * @param  cnt 扇区个数</span></span><br><span class="line"><span class="comment">  * @retval 返回错误状态;0,正常;其他,错误代码;</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">uint8_t</span> <span class="title function_">SD_WriteDisk</span><span class="params">(<span class="type">uint8_t</span> *buf, <span class="type">uint32_t</span> sector, <span class="type">uint32_t</span> cnt)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> sta = HAL_OK;</span><br><span class="line">    <span class="type">uint32_t</span> timeout = SD_TIMEOUT;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> lsector = sector;</span><br><span class="line">    INTX_DISABLE(); <span class="comment">// 关闭总中断(POLLING模式,严禁中断打断SDIO读写操作!!!)</span></span><br><span class="line">    sta = HAL_SD_WriteBlocks(&amp;hsd, (<span class="type">uint8_t</span> *)buf, lsector, cnt, SD_TIMEOUT); <span class="comment">// 多个sector的写操作</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 等待SD卡写完</span></span><br><span class="line">    <span class="keyword">while</span> (HAL_SD_GetCardState(&amp;hsd) != HAL_SD_CARD_TRANSFER)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (timeout-- == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            sta = SD_TRANSFER_BUSY;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    INTX_ENABLE(); <span class="comment">// 开启总中断</span></span><br><span class="line">    <span class="keyword">return</span> sta;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>【函数说明】</strong>在指定地址开始写入指定长度的数据。</p>
<p><strong>【参数说明】</strong></p>
<ul>
<li>buf ：写数据缓存区</li>
<li>sector ：扇区地址</li>
<li>cnt ：扇区个数</li>
</ul>
<p>	</p>
<h2 id="2-diskio-c文件的移植-1"><a href="#2-diskio-c文件的移植-1" class="headerlink" title="2. diskio.c文件的移植"></a><font size=3>2. diskio.c文件的移植</font></h2><h3 id="2-1-drive-number-1"><a href="#2-1-drive-number-1" class="headerlink" title="2.1 drive number"></a><font size=3>2.1 drive number</font></h3><p>前边移植SPI FLASH的时候已经预留了：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_SDCARD      0	<span class="comment">/* Example: Map SD card to physical drive 0 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEV_SPI_FLASH   1	<span class="comment">/* Example: Map SPI FLASH to physical drive 1 */</span></span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-disk-initialize-1"><a href="#2-2-disk-initialize-1" class="headerlink" title="2.2 disk_initialize()"></a><font size=3>2.2 disk_initialize()</font></h3><h4 id="2-2-1-函数声明-1"><a href="#2-2-1-函数声明-1" class="headerlink" title="2.2.1 函数声明"></a><font size=3>2.2.1 函数声明</font></h4><p>首先是初始化函数disk_initialize()，我们可以参考文档<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dinit.html">FatFs - disk_initialize (elm-chan.org)</a>：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_initialize</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv           <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p>这个函数初始化存储设备，并使其准备好进行通用读&#x2F;写。</p>
<h4 id="2-2-2-对SD卡初始化"><a href="#2-2-2-对SD卡初始化" class="headerlink" title="2.2.2 对SD卡初始化"></a><font size=3>2.2.2 对SD卡初始化</font></h4><p>我们来看一下对SD卡的初始化操作：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">    <span class="comment">//MX_SDIO_SD_Init();// SDIO初始化</span></span><br><span class="line">    <span class="comment">//show_sdcard_info();</span></span><br><span class="line">    result = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-2-3-移植结果-1"><a href="#2-2-3-移植结果-1" class="headerlink" title="2.2.3 移植结果"></a><font size=3>2.2.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_initialize</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">			<span class="comment">//MX_SDIO_SD_Init();// SDIO初始化</span></span><br><span class="line">			<span class="comment">//show_sdcard_info();</span></span><br><span class="line">			result = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> STA_NOINIT; <span class="comment">//初始化失败</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-disk-status-1"><a href="#2-3-disk-status-1" class="headerlink" title="2.3 disk_status()"></a><font size=3>2.3 disk_status()</font></h3><h4 id="2-3-1-函数声明-1"><a href="#2-3-1-函数声明-1" class="headerlink" title="2.3.1 函数声明"></a><font size=3>2.3.1 函数声明</font></h4><p>这个函数主要是获取设备的状态，其实这个函数并不重要，我们甚至可以让它永远返回一个成功测标志。我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dstat.html">FatFs - disk_status (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_status</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv     <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-3-2-对SD卡的状态检测"><a href="#2-3-2-对SD卡的状态检测" class="headerlink" title="2.3.2 对SD卡的状态检测"></a><font size=3>2.3.2 对SD卡的状态检测</font></h4><p>这里就没检测了，直接返回OK：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">    result = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-3-3-移植结果-1"><a href="#2-3-3-移植结果-1" class="headerlink" title="2.3.3 移植结果"></a><font size=3>2.3.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">DSTATUS <span class="title function_">disk_status</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">			result = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> STA_NOINIT;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-4-disk-read-1"><a href="#2-4-disk-read-1" class="headerlink" title="2.4 disk_read()"></a><font size=3>2.4 disk_read()</font></h3><h4 id="2-4-1-函数声明-1"><a href="#2-4-1-函数声明-1" class="headerlink" title="2.4.1 函数声明"></a><font size=3>2.4.1 函数声明</font></h4><p>这个函数主要是读取数据，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dread.html">FatFs - disk_read (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_read</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,     <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">  BYTE* buff,    <span class="comment">/* [OUT] Pointer to the read data buffer */</span></span></span><br><span class="line"><span class="params">  LBA_t sector,  <span class="comment">/* [IN] Start sector number */</span></span></span><br><span class="line"><span class="params">  UINT count     <span class="comment">/* [IN] Number of sectros to read */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>buff ：指向字节数组中用于存储读取数据的第一个数据的指针。读取数据的大小将是 扇区大小x字节数。</p>
</li>
<li><p>sector ：启动扇区号。数据类型 LBA_t 是 DWORD 或 QWORD 的别名，具体取决于配置选项。</p>
</li>
<li><p>count ：要读取的扇区数。</p>
</li>
</ul>
<p><strong>【注意事项】</strong>FatFs每次操作，都是以扇区为基本单位的。</p>
<h4 id="2-4-2-从SD卡读取"><a href="#2-4-2-从SD卡读取" class="headerlink" title="2.4.2 从SD卡读取"></a><font size=3>2.4.2 从SD卡读取</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">    result = SD_ReadDisk(buff, sector, count);	<span class="comment">// 成功时返回0;</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>



<h4 id="2-4-3-移植结果-1"><a href="#2-4-3-移植结果-1" class="headerlink" title="2.4.3 移植结果"></a><font size=3>2.4.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_read</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv,	  <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">	BYTE *buff,	  <span class="comment">/* Data buffer to store read data */</span></span></span><br><span class="line"><span class="params">	LBA_t sector, <span class="comment">/* Start sector in LBA */</span></span></span><br><span class="line"><span class="params">	UINT count	  <span class="comment">/* Number of sectors to read */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">			result = SD_ReadDisk(buff, sector, count);	<span class="comment">// 成功时返回0;</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-5-disk-write-1"><a href="#2-5-disk-write-1" class="headerlink" title="2.5 disk_write()"></a><font size=3>2.5 disk_write()</font></h3><h4 id="2-5-1-函数声明-1"><a href="#2-5-1-函数声明-1" class="headerlink" title="2.5.1 函数声明"></a><font size=3>2.5.1 函数声明</font></h4><p>这个函数主要是写入数据，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dwrite.html">FatFs - disk_write (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_write</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,        <span class="comment">/* [IN] Physical drive number */</span></span></span><br><span class="line"><span class="params">  <span class="type">const</span> BYTE* buff, <span class="comment">/* [IN] Pointer to the data to be written */</span></span></span><br><span class="line"><span class="params">  LBA_t sector,     <span class="comment">/* [IN] Sector number to write from */</span></span></span><br><span class="line"><span class="params">  UINT count        <span class="comment">/* [IN] Number of sectors to write */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>buff ：指向字节数组中用于存储写入数据的第一个数据的指针。写入数据的大小将是 扇区大小x字节数。</p>
</li>
<li><p>sector ：启动扇区号。数据类型 LBA_t 是 DWORD 或 QWORD 的别名，具体取决于配置选项。</p>
</li>
<li><p>count ：要读取的扇区数。</p>
</li>
</ul>
<p><strong>【注意事项】</strong>FatFs每次操作，都是以扇区为基本单位的。</p>
<h4 id="2-5-2-向SD卡写入"><a href="#2-5-2-向SD卡写入" class="headerlink" title="2.5.2 向SD卡写入"></a><font size=3>2.5.2 向SD卡写入</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">    result =SD_WriteDisk((<span class="type">uint8_t</span>*)buff, sector, count);</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<p>这里与前边的读一样，都是以扇区为单位操作，将参数传入底层写入函数的时候需要转换为相应的地址。</p>
<h4 id="2-5-3-移植结果-1"><a href="#2-5-3-移植结果-1" class="headerlink" title="2.5.3 移植结果"></a><font size=3>2.5.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_write</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv,		  <span class="comment">/* Physical drive nmuber to identify the drive */</span></span></span><br><span class="line"><span class="params">	<span class="type">const</span> BYTE *buff, <span class="comment">/* Data to be written */</span></span></span><br><span class="line"><span class="params">	LBA_t sector,	  <span class="comment">/* Start sector in LBA */</span></span></span><br><span class="line"><span class="params">	UINT count		  <span class="comment">/* Number of sectors to write */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">			result =SD_WriteDisk((<span class="type">uint8_t</span>*)buff, sector, count);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-6-disk-ioctl-1"><a href="#2-6-disk-ioctl-1" class="headerlink" title="2.6 disk_ioctl()"></a><font size=3>2.6 disk_ioctl()</font></h3><h4 id="2-6-1-函数声明-1"><a href="#2-6-1-函数声明-1" class="headerlink" title="2.6.1 函数声明"></a><font size=3>2.6.1 函数声明</font></h4><p>这个函数主要是用于控制设备，可根据不同命令执行不同功能，我们可以参考：<a target="_blank" rel="noopener" href="http://elm-chan.org/fsw/ff/doc/dioctl.html">FatFs - disk_ioctl (elm-chan.org)</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_ioctl</span> <span class="params">(</span></span><br><span class="line"><span class="params">  BYTE pdrv,     <span class="comment">/* [IN] Drive number */</span></span></span><br><span class="line"><span class="params">  BYTE cmd,      <span class="comment">/* [IN] Control command code */</span></span></span><br><span class="line"><span class="params">  <span class="type">void</span>* buff     <span class="comment">/* [I/O] Parameter and data buffer */</span></span></span><br><span class="line"><span class="params">)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>【函数参数】</strong></p>
<ul>
<li><p>pdrv ：用于标识目标设备的物理驱动器号。</p>
</li>
<li><p>cmd ：命令编号，比如GET_SECTOR_COUNT、GET_SECTOR_SIZE、GET_BLOCK_SIZE等，详细的命令及含义可以参考官方文档说明。</p>
</li>
<li><p>buff ：参数的指针取决于命令代码。不要在意该命令是否没有要传递的参数。</p>
</li>
</ul>
<h4 id="2-6-2-获取SD卡信息"><a href="#2-6-2-获取SD卡信息" class="headerlink" title="2.6.2 获取SD卡信息"></a><font size=3>2.6.2 获取SD卡信息</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">    <span class="keyword">switch</span>(cmd)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">case</span> GET_SECTOR_COUNT:<span class="comment">// 扇区数量</span></span><br><span class="line">            *(DWORD *)buff = SDCardInfo.LogBlockNbr;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GET_SECTOR_SIZE:<span class="comment">// 扇区大小</span></span><br><span class="line">            *(WORD *)buff = SDCardInfo.BlockSize;<span class="comment">// 每个扇区是512B</span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> GET_BLOCK_SIZE:<span class="comment">// 每次擦除块的大小的个数</span></span><br><span class="line">            *(DWORD *)buff = SDCardInfo.LogBlockSize;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    result = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">break</span>;</span><br></pre></td></tr></table></figure>

<h4 id="2-6-3-移植结果-1"><a href="#2-6-3-移植结果-1" class="headerlink" title="2.6.3 移植结果"></a><font size=3>2.6.3 移植结果</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">DRESULT <span class="title function_">disk_ioctl</span><span class="params">(</span></span><br><span class="line"><span class="params">	BYTE pdrv, <span class="comment">/* Physical drive nmuber (0..) */</span></span></span><br><span class="line"><span class="params">	BYTE cmd,  <span class="comment">/* Control code */</span></span></span><br><span class="line"><span class="params">	<span class="type">void</span> *buff <span class="comment">/* Buffer to send/receive control data */</span></span></span><br><span class="line"><span class="params">)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> result = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (pdrv)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">case</span> DEV_SDCARD:</span><br><span class="line">			<span class="keyword">switch</span>(cmd)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">case</span> GET_SECTOR_COUNT:<span class="comment">// 扇区数量</span></span><br><span class="line">					*(DWORD *)buff = SDCardInfo.LogBlockNbr;</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				<span class="keyword">case</span> GET_SECTOR_SIZE:<span class="comment">// 扇区大小</span></span><br><span class="line">					*(WORD *)buff = SDCardInfo.BlockSize;<span class="comment">// 每个扇区是512B</span></span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				<span class="keyword">case</span> GET_BLOCK_SIZE:<span class="comment">// 每次擦除块的大小的个数</span></span><br><span class="line">					*(DWORD *)buff = SDCardInfo.LogBlockSize;</span><br><span class="line">				<span class="keyword">default</span>:</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			result = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			result = <span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(result == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span> RES_OK;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> RES_ERROR;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-ffconf-h文件的配置-1"><a href="#3-ffconf-h文件的配置-1" class="headerlink" title="3. ffconf.h文件的配置"></a><font size=3>3. ffconf.h文件的配置</font></h2><p>这里的配置基本不变。</p>
<h3 id="3-1-FF-VOLUMES-1"><a href="#3-1-FF-VOLUMES-1" class="headerlink" title="3.1 FF_VOLUMES"></a><font size=3>3.1 FF_VOLUMES</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_VOLUMES		2</span></span><br><span class="line"><span class="comment">/* Number of volumes (logical drives) to be used. (1-10) */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-FF-MAX-SS-1"><a href="#3-2-FF-MAX-SS-1" class="headerlink" title="3.2 FF_MAX_SS"></a><font size=3>3.2 FF_MAX_SS</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_MIN_SS		512</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> FF_MAX_SS		4096</span></span><br><span class="line"><span class="comment">/* This set of options configures the range of sector size to be supported. (512,</span></span><br><span class="line"><span class="comment">/  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and</span></span><br><span class="line"><span class="comment">/  harddisk, but a larger value may be required for on-board flash memory and some</span></span><br><span class="line"><span class="comment">/  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured</span></span><br><span class="line"><span class="comment">/  for variable sector size mode and disk_ioctl() function needs to implement</span></span><br><span class="line"><span class="comment">/  GET_SECTOR_SIZE command. */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-3-FF-USE-MKFS-1"><a href="#3-3-FF-USE-MKFS-1" class="headerlink" title="3.3 FF_USE_MKFS"></a><font size=3>3.3 FF_USE_MKFS</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> FF_FS_READONLY  0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> FF_USE_MKFS		1</span></span><br><span class="line"><span class="comment">/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */</span></span><br></pre></td></tr></table></figure>

<h1 id="四、一些测试函数"><a href="#四、一些测试函数" class="headerlink" title="四、一些测试函数"></a><font size=3>四、一些测试函数</font></h1><p>最终的代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/stm32f103-prj">STM32F103-Prj: STM32学习使用（STM32CubeMX+Makefile+VScode+J-Flash） (gitee.com)</a></p>
<h2 id="1-文件系统结构体定义"><a href="#1-文件系统结构体定义" class="headerlink" title="1. 文件系统结构体定义"></a><font size=3>1. 文件系统结构体定义</font></h2><p>为了方便管理文件系统，定义一个结构体：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> __<span class="title">ex_fatfs</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    FATFS *fs[FF_VOLUMES]; <span class="comment">// 逻辑磁盘工作区.</span></span><br><span class="line">    BYTE *work;  <span class="comment">// 挂载的时候用</span></span><br><span class="line">    FIL *file;             <span class="comment">// 文件1</span></span><br><span class="line">    FIL *ftemp;            <span class="comment">// 文件2</span></span><br><span class="line">    UINT br;               <span class="comment">// 读指针</span></span><br><span class="line">    UINT bw;               <span class="comment">// 写指针</span></span><br><span class="line">    FILINFO fileinfo;      <span class="comment">// 文件信息</span></span><br><span class="line">    DIR dir;               <span class="comment">// 目录</span></span><br><span class="line">&#125; EX_FATFS_PARAM;</span><br></pre></td></tr></table></figure>

<h2 id="2-申请内存"><a href="#2-申请内存" class="headerlink" title="2. 申请内存"></a><font size=3>2. 申请内存</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  为文件系统所用变量申请内存</span></span><br><span class="line"><span class="comment">  * @note   </span></span><br><span class="line"><span class="comment">  * @param  </span></span><br><span class="line"><span class="comment">  * @retval 0，申请成功，-1，申请失败</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">int8_t</span> <span class="title function_">exf_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> i;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; FF_VOLUMES; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        exFatfsParam.fs[i] = (FATFS *)pub_malloc(SRAMIN, <span class="keyword">sizeof</span>(FATFS)); <span class="comment">// 为磁盘i工作区申请内存</span></span><br><span class="line">        <span class="keyword">if</span> (!exFatfsParam.fs[i])</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    exFatfsParam.file = (FIL *)pub_malloc(SRAMEX, <span class="keyword">sizeof</span>(FIL));   <span class="comment">// 为file申请内存</span></span><br><span class="line">    exFatfsParam.ftemp = (FIL *)pub_malloc(SRAMEX, <span class="keyword">sizeof</span>(FIL));  <span class="comment">// 为ftemp申请内存</span></span><br><span class="line">    exFatfsParam.work = (<span class="type">uint8_t</span> *)pub_malloc(SRAMEX, FF_MAX_SS); <span class="comment">// 为work申请内存</span></span><br><span class="line">    <span class="keyword">if</span> (i == FF_VOLUMES &amp;&amp; exFatfsParam.file &amp;&amp; exFatfsParam.ftemp &amp;&amp; exFatfsParam.work)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 申请有一个失败,即失败.</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-获取磁盘容量"><a href="#3-获取磁盘容量" class="headerlink" title="3. 获取磁盘容量"></a><font size=3>3. 获取磁盘容量</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * @brief  得到磁盘剩余容量</span></span><br><span class="line"><span class="comment">  * @note   好像是去掉了FatFs自身占据的空间</span></span><br><span class="line"><span class="comment">  * @param  drv 磁盘编号(&quot;0:&quot;/&quot;1:&quot;)</span></span><br><span class="line"><span class="comment">  * @param  total 总容量 （单位KB）</span></span><br><span class="line"><span class="comment">  * @param  free 剩余容量 （单位KB）</span></span><br><span class="line"><span class="comment">  * @retval 0,正常.其他,错误代码</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="type">uint8_t</span> <span class="title function_">exf_getfree</span><span class="params">(<span class="type">uint8_t</span> *drv, <span class="type">uint32_t</span> *total, <span class="type">uint32_t</span> *<span class="built_in">free</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    FATFS *fs1;</span><br><span class="line">    <span class="type">uint8_t</span> res;</span><br><span class="line">    <span class="type">uint32_t</span> fre_clust = <span class="number">0</span>, fre_sect = <span class="number">0</span>, tot_sect = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">// 得到磁盘信息及空闲簇数量</span></span><br><span class="line">    res = (<span class="type">uint32_t</span>)f_getfree((<span class="type">const</span> TCHAR *)drv, (DWORD *)&amp;fre_clust, &amp;fs1);</span><br><span class="line">    <span class="keyword">if</span> (res == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        tot_sect = (fs1-&gt;n_fatent - <span class="number">2</span>) * fs1-&gt;csize; <span class="comment">// 得到总扇区数</span></span><br><span class="line">        fre_sect = fre_clust * fs1-&gt;csize;           <span class="comment">// 得到空闲扇区数</span></span><br><span class="line">		<span class="comment">//printf(&quot;fs1-&gt;n_fatent %d, fs1-&gt;csize %d, tot_sect %d, fre_sect %d\r\n&quot;, fs1-&gt;n_fatent, fs1-&gt;csize, tot_sect, fre_sect);</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> FF_MAX_SS != 512                                 <span class="comment">// 扇区大小不是512字节,则转换为512字节</span></span></span><br><span class="line">        tot_sect *= fs1-&gt;ssize / <span class="number">512</span>;</span><br><span class="line">        fre_sect *= fs1-&gt;ssize / <span class="number">512</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        *total = tot_sect &gt;&gt; <span class="number">1</span>; <span class="comment">// 单位为KB</span></span><br><span class="line">        *<span class="built_in">free</span> = fre_sect &gt;&gt; <span class="number">1</span>;  <span class="comment">// 单位为KB</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/fc1f4d1a.html">LV16-22-FatFs-02-FatFs文件系统移植</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年06月04日 - 22:07</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/fc1f4d1a.html" title="LV16-22-FatFs-02-FatFs文件系统移植">https://sumumm.github.io/post/fc1f4d1a.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV16-STM32%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV16-STM32开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/7e7fa731.html" rel="prev" title="LV16-23-WDG-01-看门狗基础知识">
                  <i class="fa fa-angle-left"></i> LV16-23-WDG-01-看门狗基础知识
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/d715f558.html" rel="next" title="LV16-22-FatFs-01-FatFs文件系统">
                  LV16-22-FatFs-01-FatFs文件系统 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
